#include "CubeMesh.h"

#include "Render/BlockMesh.h"
#include "Render/BlockVertex.h"

CubeMesh::CubeMesh() {
  std::vector<BlockVertex> vertices;

  vertices.resize(6 * 6);
  int32_t vertexCount = 0;
  for (const auto& face: BlockMesh::vertices) {
    for (const auto& vertex: face) {
      vertices.at(vertexCount) = vertex;
      vertexCount++;
    }
  }
  m_cubeVertexArray = CreateRef<VertexArray>(vertices, BlockVertex::VertexAttributes());
}

CubeMesh::CubeMesh(const Ref<BlockModel> &model)
  : m_blockModel(model)
{
  std::vector<BlockVertex> vertices;
  auto &verticesFromModel = model->GetVertices();
  vertices.resize(verticesFromModel.size());

  for (int i = 0; i < verticesFromModel.size(); i += 6) {
    // 四条线
    vertices.push_back(verticesFromModel[i + 0]);
    vertices.push_back(verticesFromModel[i + 1]);

    vertices.push_back(verticesFromModel[i + 1]);
    vertices.push_back(verticesFromModel[i + 4]);

    vertices.push_back(verticesFromModel[i + 4]);
    vertices.push_back(verticesFromModel[i + 2]);

    vertices.push_back(verticesFromModel[i + 2]);
    vertices.push_back(verticesFromModel[i + 0]);

  }
  m_cubeVertexArray = CreateRef<VertexArray>(vertices, BlockVertex::VertexAttributes());
}

void CubeMesh::Render(int32_t type) const {
  m_cubeVertexArray->DrawVertex(type);
}